package com.difsonapps.android.marker.adapters;

import com.difsonapps.android.marker.FeedImageView;
import com.difsonapps.android.marker.R;
import com.difsonapps.android.marker.activities.MarksActivity;
import com.difsonapps.android.marker.app.AppController;
import com.difsonapps.android.marker.beans.Album;
 
import java.util.List;
 
import android.app.Activity;
import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
 
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
 
public class MarksValuesGridAdapter extends BaseAdapter {  
    private MarksActivity activity;
    private LayoutInflater inflater;
    private int nbValues;
    ImageLoader imageLoader = AppController.getInstance().getImageLoader();
    static int[] images = new int[]{R.drawable.ic0, R.drawable.ic1, R.drawable.ic2, R.drawable.ic3, R.drawable.ic4, R.drawable.ic5, R.drawable.ic6, R.drawable.ic7, R.drawable.ic8, R.drawable.ic9, R.drawable.ic10};
    static int[] imagesSelected = new int[]{R.drawable.ics0, R.drawable.ics1, R.drawable.ics2, R.drawable.ics3, R.drawable.ics4, R.drawable.ics5, R.drawable.ics6, R.drawable.ics7, R.drawable.ics8, R.drawable.ics9, R.drawable.ics10};
    int currentSelected = -1;
    
    public MarksValuesGridAdapter(MarksActivity activity) {
    	this(activity, 11);
    }
 
    public MarksValuesGridAdapter(MarksActivity activity, int nbValues) {
        this.activity = activity;
        this.nbValues = nbValues;
    }
 
    @Override
    public int getCount() {
        return nbValues;
    }
 
    @Override
    public Object getItem(int location) {
        return location;
    }
 
    @Override
    public long getItemId(int position) {
        return position;
    }
    
    public static int getImage(int position){
    	return images[position];
    }
    
    public static int getSelectedImage(int position){
    	return imagesSelected[position];
    }
    
    public void setSelectedValue(int value){
    	currentSelected = value;
    }
    
    public int getSelectedValue(){
    	return currentSelected;
    }

	@Override
    public View getView(int position, View convertView, ViewGroup parent) {
 
        if (inflater == null)
            inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null)
            convertView = inflater.inflate(R.layout.mark_button_item, null);
 
        /*if (imageLoader == null)
            imageLoader = AppController.getInstance().getImageLoader();
         */
        ImageButton button = /*new ImageButton(activity);/*/(ImageButton)convertView.findViewById(R.id.markValueImageButton); //TODO
        button.setImageResource(position==currentSelected?imagesSelected[position]:images[position]);
        button.setOnClickListener(new MarkValueClick(position)); 
        
        
 
        return convertView;
    }
    
    class MarkValueClick implements OnClickListener{
    	int position;
    	
    	
    	public MarkValueClick(int position) {
			super();
			this.position = position;
		}


		@Override
		public void onClick(View arg0) {
			activity.selectMarkValue(position);
			currentSelected = position;
			notifyDataSetChanged();
		}
    	
    }
 
}
